package cc.realxyq.springboot.db.mongodb.controller;

import cc.realxyq.springboot.db.mongodb.baens.MongoUser;
import cc.realxyq.springboot.db.mongodb.dao.MongoDbUserDao;
import cc.realxyq.springboot.db.utils.JsonResult;
import cc.realxyq.springboot.db.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : [buleing]
 * @version : [v1.0]
 * @description : [一句话描述该类的功能]
 * @createTime : [2021/6/11 17:48]
 * @updateUser : [buleing]
 * @updateTime : [2021/6/11 17:48]
 * @updateRemark : [说明本次修改内容]
 */
@RestController
public class MongoController {

    @Autowired
    private MongoDbUserDao mongoDao;

    /**
     * @Description:  描述啊啊啊
     * @param user 对象参数
     * @return: JsonResult
     * @Author: （填写你自己的用户名）
     * @Date: 2021/6/11 18:13
     */
    @PostMapping("/mongo/user")
    public JsonResult save(MongoUser user){
        user = mongoDao.save(user);
        return null==user?new JsonResult(ResultCode.SUCCESS_FAIL):new JsonResult(ResultCode.SUCCESS,user);

    }

    @GetMapping("/mongo/users/name/{name}")
    public JsonResult find(@PathVariable("name") String username){
        MongoUser user = new MongoUser();
        user.setName(username);
        List<MongoUser> list = mongoDao.findByName(user);
        return list.size()>0?new JsonResult(ResultCode.SUCCESS,list):new JsonResult(ResultCode.SUCCESS_NO_DATA);

    }

}
